package com.example.school.controller.admin;

import com.example.school.dto.AddressUpdateDto;
import com.example.school.dto.AdminGetListDto;
import com.example.school.dto.SaveAddressDto;
import com.example.school.service.AddressService;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/address")
public class AdminAddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/get_admin_list")
    public Result getAdminList(@RequestBody AdminGetListDto dto){
        return addressService.getAdminList(dto);
    }

    @PostMapping("/save_address")
    public Result saveAddress(@RequestBody SaveAddressDto dto){
        return addressService.saveAddress(dto);
    }

    @GetMapping("/remove_address")
    public Result removeAddress(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = addressService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/get_update_address")
    public Result getUpdateAddress(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(addressService.getById(id));
    }

    @PostMapping("/update_address")
    public Result updateAddress(@RequestBody AddressUpdateDto dto){
        return addressService.updateAddress(dto);
    }

}
